package com.beemans.common.ext

import android.app.Activity
import android.app.Dialog
import android.view.View
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import com.beemans.common.R
import com.beemans.common.data.constant.CommonStore
import com.gyf.immersionbar.ImmersionBar
import com.tiamosu.fly.base.BaseFlyActivity
import com.tiamosu.fly.base.BaseFlyFragment
import com.tiamosu.fly.base.dialog.BaseFlyDialogFragment

/**
 * @author tiamosu
 * @date 2021/3/18.
 */

/**
 * 沉浸式配置，设置导航栏颜色为白色
 */
inline fun BaseFlyActivity.immersionBarExt(block: ImmersionBar.() -> Unit = {}) {
    immersionBar {
        statusBarColor(R.color.transparent)
        statusBarDarkFont(false)
        if (hasNavigationBar) {
            navigationBarColor(R.color.white)
            navigationBarDarkIcon(true)
        }
        block.invoke(this)
        CommonStore.immersionBarParams = barParams
    }
}

/**
 * 沉浸式配置，设置导航栏颜色为白色
 */
inline fun BaseFlyFragment.immersionBarExt(block: ImmersionBar.() -> Unit = {}) {
    immersionBar {
        statusBarColor(R.color.transparent)
        statusBarDarkFont(false)
        if (hasNavigationBar) {
            navigationBarColor(R.color.white)
            navigationBarDarkIcon(true)
        }
        block.invoke(this)
        CommonStore.immersionBarParams = barParams
    }
}

/**
 * 沉浸式配置，设置导航栏颜色为白色
 */
inline fun BaseFlyDialogFragment.immersionBarExt(block: ImmersionBar.() -> Unit = {}) {
    immersionBar {
        val params = CommonStore.immersionBarParams
        statusBarColor(R.color.transparent)
        statusBarDarkFont(params?.statusBarDarkFont ?: true)
        if (hasNavigationBar) {
            navigationBarColor(R.color.transparent)
            navigationBarDarkIcon(params?.navigationBarDarkIcon ?: true)
        }
        block.invoke(this)
        CommonStore.immersionBarParams = barParams
    }
}

/**
 * 初始化ImmersionBar
 */
inline fun Activity.immersionBar(block: ImmersionBar.() -> Unit) =
    ImmersionBar.with(this).apply { block(this) }.init()

inline fun Fragment.immersionBar(block: ImmersionBar.() -> Unit) =
    ImmersionBar.with(this).apply { block(this) }.init()

inline fun DialogFragment.immersionBar(block: ImmersionBar.() -> Unit) =
    ImmersionBar.with(this).apply { block(this) }.init()

inline fun Dialog.immersionBar(activity: Activity, block: ImmersionBar.() -> Unit) =
    ImmersionBar.with(activity, this).apply { block(this) }.init()

inline fun Activity.immersionBar(dialog: Dialog, block: ImmersionBar.() -> Unit) =
    ImmersionBar.with(this, dialog).apply { block(this) }.init()

inline fun Fragment.immersionBar(dialog: Dialog, block: ImmersionBar.() -> Unit) =
    activity?.run { ImmersionBar.with(this, dialog).apply { block(this) }.init() }
        ?: Unit

fun Activity.immersionBar() = immersionBar { }
fun Fragment.immersionBar() = immersionBar { }
fun DialogFragment.immersionBar() = immersionBar { }

fun Dialog.immersionBar(activity: Activity) = immersionBar(activity) {}
fun Activity.immersionBar(dialog: Dialog) = immersionBar(dialog) {}
fun Fragment.immersionBar(dialog: Dialog) = immersionBar(dialog) {}

/**
 * 隐藏状态栏
 */
fun Activity.hideStatusBar() = ImmersionBar.hideStatusBar(window)
fun Fragment.hideStatusBar() = activity?.run { ImmersionBar.hideStatusBar(window) } ?: Unit

/**
 * 显示状态栏
 */
fun Activity.showStatusBar() = ImmersionBar.showStatusBar(window)
fun Fragment.showStatusBar() = activity?.run { ImmersionBar.showStatusBar(window) } ?: Unit

/**
 * 解决顶部与布局重叠问题，不可逆
 */
fun Activity.setFitsSystemWindows() = ImmersionBar.setFitsSystemWindows(this)
fun Fragment.setFitsSystemWindows() = ImmersionBar.setFitsSystemWindows(this)

/**
 * titleBar扩展
 */
fun Activity.fitsTitleBar(vararg view: View) = ImmersionBar.setTitleBar(this, *view)
fun Fragment.fitsTitleBar(vararg view: View) = ImmersionBar.setTitleBar(this, *view)

fun Activity.fitsTitleBarMarginTop(vararg view: View) =
    ImmersionBar.setTitleBarMarginTop(this, *view)

fun Fragment.fitsTitleBarMarginTop(vararg view: View) =
    ImmersionBar.setTitleBarMarginTop(this, *view)

/**
 * statusBarView扩展
 */
fun Activity.fitsStatusBarView(view: View) = ImmersionBar.setStatusBarView(this, view)
fun Fragment.fitsStatusBarView(view: View) = ImmersionBar.setStatusBarView(this, view)

/**
 * 导航栏是否在底部
 */
val Activity.isNavigationAtBottom get() = ImmersionBar.isNavigationAtBottom(this)
val Fragment.isNavigationAtBottom get() = ImmersionBar.isNavigationAtBottom(this)

/**
 * 是否支持状态栏字体变色
 */
val isSupportStatusBarDarkFont get() = ImmersionBar.isSupportStatusBarDarkFont()

/**
 * 师傅支持导航栏图标
 */
val isSupportNavigationIconDark get() = ImmersionBar.isSupportNavigationIconDark()

/**
 * 检查view是否使用了fitsSystemWindows
 */
val View.checkFitsSystemWindows get() = ImmersionBar.checkFitsSystemWindows(this)

/**
 * 获得刘海屏高度
 */
val Activity.notchHeight get() = ImmersionBar.getNotchHeight(this)
val Fragment.notchHeight get() = ImmersionBar.getNotchHeight(this)

/**
 * 是否有刘海屏
 */
val Activity.hasNotchScreen get() = ImmersionBar.hasNotchScreen(this)
val Fragment.hasNotchScreen get() = ImmersionBar.hasNotchScreen(this)
val View.hasNotchScreen get() = ImmersionBar.hasNotchScreen(this)

/**
 * 是否有导航栏
 */
val Activity.hasNavigationBar get() = ImmersionBar.hasNavigationBar(this)
val Fragment.hasNavigationBar get() = ImmersionBar.hasNavigationBar(this)

/**
 * ActionBar扩展
 */
val Activity.actionBarHeight get() = ImmersionBar.getActionBarHeight(this)
val Fragment.actionBarHeight get() = ImmersionBar.getActionBarHeight(this)

/**
 * 导航栏扩展
 */
val Activity.navigationBarHeight get() = ImmersionBar.getNavigationBarHeight(this)
val Fragment.navigationBarHeight get() = ImmersionBar.getNavigationBarHeight(this)

val Activity.navigationBarWidth get() = ImmersionBar.getNavigationBarWidth(this)
val Fragment.navigationBarWidth get() = ImmersionBar.getNavigationBarWidth(this)

/**
 * 状态栏扩展
 */
val Activity.statusBarHeight get() = ImmersionBar.getStatusBarHeight(this)
val Fragment.statusBarHeight get() = ImmersionBar.getStatusBarHeight(this)

/**
 * dialog销毁
 */
fun Activity.destroyImmersionBar(dialog: Dialog) = ImmersionBar.destroy(this, dialog)
fun Fragment.destroyImmersionBar(dialog: Dialog) =
    activity?.run { ImmersionBar.destroy(this, dialog) } ?: Unit